#include "GlobalSettings.h"
#include "Communicator.h"

using namespace erep;

Communicator::Communicator( const QString& pathWithArg, QObject* parent )
:   QObject(parent), _count(0)
{
    _pathWithArgument = pathWithArg;
    _network = new QNetworkAccessManager( this );
    connect( _network, SIGNAL(finished(QNetworkReply*)), SIGNAL(readyRead(QNetworkReply*)) );
    connect( _network, SIGNAL(finished(QNetworkReply*)), SLOT(recount(QNetworkReply*)) );
}

Communicator::~Communicator()
{
}

QNetworkReply* Communicator::get( const QString& argument )
{
    QNetworkRequest request;
    request.setUrl( _pathWithArgument.arg(argument) );
    _count++;
    return _network->get( request );
}

QNetworkReply* Communicator::post( const QString& argument, const QByteArray& data )
{
    QNetworkRequest request;
    request.setUrl( _pathWithArgument.arg(argument) );
    _count++;
    return _network->post( request, data );
}

void Communicator::recount( QNetworkReply* )
{
    if ( _count>0 )
        _count--;
}
